All files / src/app/api/items/[id]/remove-from-sorts route.ts

90.9% Statements 10/11
100% Branches 2/2
100% Functions 1/1
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                  6x 6x 6x 6x     6x 6x   3x   3x   3x          
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { getDb } from "@/lib/db";
import { requireApiItemCapability } from "@/server/dal/collection-access";
import { SortResultService } from "@/server/services/SortResultService";
import { errorResponse } from "@/server/api/http-errors";
 
// Owner-only bulk-detach: remove an item from all DRAFT sort results in one call,
// the purge-prep companion to POST /api/items/[id]/purge. A deliberate t9l
// exception (a collection-owner op crossing the per-curator boundary; draft-only,
// finalized untouched). No confirm body: the op is re-derivable by regenerating
// the affected draft. Authz BEFORE any body
// access: a non-owner never reaches the service.
export async function POST(
  _request: NextRequest,
  { params }: { params: Promise<{ id: string }> },
) {
  try {
    const { id } = await params;
    const user = await requireAuth();
    const db = getDb();
 
    // items:purge = owner. includeDeleted: true — item may be soft-deleted pre-purge.
    const item = await requireApiItemCapability(db, id, user.id, "items:purge", { includeDeleted: true });
    if (item instanceof NextResponse) return item; // 404 (absent) / 403 (not owner)
 
    const service = SortResultService.fromDb(db);
    const { removed, remainingInFinalized } =
      await service.removeItemFromDraftResults(item.collectionId, item.id);
 
    return NextResponse.json({ removed, remainingInFinalized });
  } catch (error) {
    return errorResponse(error);
  }
}